﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
using Traceability.Common.Domain;
using Traceability.Common.Dtos;
using Traceability.Common.Infrastructure.InterFaces;
using Traceability.Enum;

namespace Traceability.Bais.API.Appliction.Command.Users
{
    public class UserListHandler : IRequestHandler<UserListCommon, Result<User>>
    {
        private readonly IBaseRepository<User> userRep;
       
        public UserListHandler(IBaseRepository<User> _userRep)
        {
            userRep = _userRep;
        }
        public async Task<Result<User>> Handle(UserListCommon request, CancellationToken cancellationToken)
        {
            var Result = new Result<User>() { Code = (int)CommonErrorCode.search };
            try
            {
                var list = await (await userRep.GetAll()).FirstOrDefaultAsync(c=>c.UserName==(request.UserName) &&c.UserPwd==request.UserPwd);
               
                if (list != null)
                {
                    Result.list = list;
                }
                else
                {
                    Result.Code = (int)CommonErrorCode.error;
                }
               

            }
            catch (Exception)
            {
                Result.Msg = "登录失败";
                throw;
            }

            return Result;

        }

    }
}
